var count = 0;
$(function(){
    screenFuc();
	})
    function screenFuc() {
        var topHeight = $(".chatBox-head").innerHeight();//聊天头部高度
        //屏幕小于768px时候,布局change
        var winWidth = $(window).innerWidth();
        if (winWidth <= 768) {
            var totalHeight = $(window).height(); //页面整体高度
            $(".chatBox-info").css("height", totalHeight - topHeight);
            var infoHeight = $(".chatBox-info").innerHeight();//聊天头部以下高度
            //中间内容高度
            $(".chatBox-content").css("height", infoHeight - 46);
            $(".chatBox-content-demo").css("height", infoHeight - 46);

            $(".chatBox-list").css("height", totalHeight - topHeight);
            $(".chatBox-kuang").css("height", totalHeight - topHeight);
            $(".div-textarea").css("width", winWidth - 106);
        } else {
            $(".chatBox-info").css("height", 495);
            $(".chatBox-content").css("height", 448);
            $(".chatBox-content-demo").css("height", 448);
            $(".chatBox-list").css("height", 495);
            $(".chatBox-kuang").css("height", 495);
            $(".div-textarea").css("width", 260);
        }
    }
    (window.onresize = function () {
        screenFuc();
    })();
	
	 //未读信息数量为空时
    var totalNum = $(".chat-message-num").html();
    if (totalNum == "") {
        $(".chat-message-num").css("padding", 0);
    }
    $(".message-num").each(function () {
        var wdNum = $(this).html();
        if (wdNum == "") {
            $(this).css("padding", 0);
        }
    });
	
//打开/关闭聊天框
    $(".chatBtn").click(function () {
        $(".chatBox").toggle(10);
    })
    $(".chat-close").click(function () {
        $(".chatBox").toggle(10);
    })
    //进聊天页面
    $(".chat-list-people").each(function () {
        $(this).click(function () {
            var n = $(this).index();
            $(".chatBox-head-one").toggle();
            $(".chatBox-head-two").toggle();
            $(".chatBox-list").fadeToggle();
            $(".chatBox-kuang").fadeToggle();
            
            //传名字
            $(".ChatInfoName").text($(this).children(".chat-name").children("p").eq(0).html());

            //传头像
            $(".ChatInfoHead>img").attr("src", $(this).children().eq(0).children("img").attr("src"));
            //聊天框默认最底部
            $(document).ready(function () {
            	$("#chatBox-content-demo").scrollTop($("#chatBox-content-demo")[0].scrollHeight);
            });
            if(n == 0){
            	init();
                //$(".chatBox-kuang")[0].style.display='block';
            	//$(".chatBox-kuang")[1].style.display='none';
            }else{
            	//$(".chatBox-kuang")[0].style.display='none';
            	//$(".chatBox-kuang")[1].style.display='block';
//            	$(".chatBox-ai").show();
            	//console.log($(".chatBox-kuang")[1])
            }
        })
    });

    //返回列表
    $(".chat-return").click(function () {
        $(".chatBox-head-one").toggle(1);
        $(".chatBox-head-two").toggle(1);
        $(".chatBox-list").fadeToggle(1);
        $(".chatBox-kuang").fadeToggle(1);
    });

    //发送信息
    $("#chat-fasong").click(function () {
        var textContent = $(".div-textarea").html().replace(/[\n\r]/g, '<br>')
        if (textContent != "") {
            $(".chatBox-content-demo").append("<div class=\"clearfloat\">" +
                "<div class=\"author-name\"><small class=\"chat-date\">"+
				getDateByFormat()+
				"</small> </div> " +
                "<div class=\"right\"> <div class=\"chat-message\"> " + textContent + " </div> " +
                "<div class=\"chat-avatars\"><img src=\"img/c57.jpg\" alt=\"头像\" /></div> </div> </div>");
            //发送后清空输入框
            $(".div-textarea").html("");
            //聊天框默认最底部
            $(document).ready(function () {
                $("#chatBox-content-demo").scrollTop($("#chatBox-content-demo")[0].scrollHeight);
            });
            var pageId = $("#pageId").val();
			var de = {
                   "pageId":pageId,
                   "question":textContent,
				};
			  $.ajax({
				type: "post",
				url: "/QTA",
				data: de,
				dataType:"json",
				//contentType: "application/x-www-form-urlencoded,X-Requested-With,Content-Type,Accept,Origin",
				success: function (data) {
					$(".chatBox-content-demo").append("<div class=\"clearfloat\">" +
						"<div class=\"author-name\"><small class=\"chat-date\">"+
						getDateByFormat()+
						"</small> </div> " +
						"<div class=\"left\">"+
						"<div class=\"chat-avatars\"><img src=\"img/mo1.png\" alt=\"头像\" /></div>"+
						" <div class=\"chat-message\"> " + data.msg + " </div>  </div>" );
					//发送后清空输入框
					$(".div-textarea").html("");
					//聊天框默认最底部
					$(document).ready(function () {
						$("#chatBox-content-demo").scrollTop($("#chatBox-content-demo")[0].scrollHeight);
					});
				
				}
			});
        }
    });

    //      发送表情
    $("#chat-biaoqing").click(function () {
        $(".biaoqing-photo").toggle();
    });
    $(document).click(function () {
        $(".biaoqing-photo").css("display", "none");
    });
    $("#chat-biaoqing").click(function (event) {
        event.stopPropagation();//阻止事件
    });

    $(".emoji-picker-image").each(function () {
        $(this).click(function () {
            var bq = $(this).parent().html();
            console.log(bq)
            $(".chatBox-content-demo").append("<div class=\"clearfloat\">" +
                "<div class=\"author-name\"><small class=\"chat-date\">"+
				getDateByFormat()+
				"</small> </div> " +
                "<div class=\"right\"> <div class=\"chat-message\"> " + bq + " </div> " +
                "<div class=\"chat-avatars\"><img src=\"img/icon01.png\" alt=\"头像\" /></div> </div> </div>");
            //发送后关闭表情框
            $(".biaoqing-photo").toggle();
            //聊天框默认最底部
            $(document).ready(function () {
                $("#chatBox-content-demo").scrollTop($("#chatBox-content-demo")[0].scrollHeight);
            });
        })
    });

    //发送图片
    function selectImg(pic) {
        if (!pic.files || !pic.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            var images = evt.target.result;
            $(".chatBox-content-demo").append("<div class=\"clearfloat\">" +
                "<div class=\"author-name\"><small class=\"chat-date\">"+
				getDateByFormat()+
				"</small> </div> " +
                "<div class=\"right\"> <div class=\"chat-message\"><img src=" + images + "></div> " +
                "<div class=\"chat-avatars\"><img src=\"img/icon01.png\" alt=\"头像\" /></div> </div> </div>");
            //聊天框默认最底部
            $(document).ready(function () {
                $("#chatBox-content-demo").scrollTop($("#chatBox-content-demo")[0].scrollHeight);
            });
        };
        reader.readAsDataURL(pic.files[0]);

    }
	$("#sssmsgfoot").keyup(function(event){
		if(event.keyCode ==13){  
                    //这里填写你要做的事件
                   //TODO
		$("#chat-fasong").trigger("click");
    } 
	});
/**
 * 
 * 获取当前时间
 */
function getNow(s) {
    return s < 10 ? '0' + s: s;
}
/**
	*  获取当前时间 格式为 2017-12-02 14:26:58
	*/
function getDateByFormat(){
	var myDate = new Date();
	//获取当前年
	var year=myDate.getFullYear();
	//获取当前月
	var month=myDate.getMonth()+1;
	//获取当前日
	var date=myDate.getDate(); 
	var h=myDate.getHours();       //获取当前小时数(0-23)
	var m=myDate.getMinutes();     //获取当前分钟数(0-59)
	var s=myDate.getSeconds();  

	var now=year+'-'+getNow(month)+"-"+getNow(date)+" "+getNow(h)+':'+getNow(m)+":"+getNow(s);
	return now;
}

function init(){
	if(count == 0){
			$(".chatBox-content-demo").append("<div class=\"clearfloat\">" +
	                "<div class=\"author-name\"><small class=\"chat-date\">"+
					getDateByFormat()+
					"</small> </div> " +
	                "<div class=\"left\">"+
	                "<div class=\"chat-avatars\"><img src=\"img/mo1.png\" alt=\"头像\" /></div> "+
					" <div class=\"chat-message\"> " + "你好! 我是小莫,很高兴认识你!" + " </div></div> " );
	            //发送后清空输入框
	            $(".div-textarea").html("");
	            //聊天框默认最底部
	            $(document).ready(function () {
	                $("#chatBox-content-demo").scrollTop($("#chatBox-content-demo")[0].scrollHeight);
	            });
				count ++;
		}
}